<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>比较运算符</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.operators.bitwise.html">« 位运算符</a></li>
      <li style="float: right;"><a href="language.operators.errorcontrol.html">错误控制运算符 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.operators.html">运算符</a></li>
    <li>比较运算符</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.operators.comparison" class="sect1">
   <h2 class="title">比较运算符</h2>
   <p class="simpara">
    比较运算符，如同它们名称所暗示的，允许对两个值进行比较。还可以参考
    <a href="types.comparisons.html" class="link">PHP 类型比较表</a>看不同类型相互比较的例子。
   </p>
   <table class="doctable table">
    <caption><strong>比较运算符</strong></caption>
    
     <thead>
      <tr>
       <th>例子</th>
       <th>名称</th>
       <th>结果</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>$a == $b</td>
       <td>等于</td>
       <td><strong><code>true</code></strong>，如果类型转换后 <var class="varname">$a</var> 等于 <var class="varname">$b</var>。</td>
      </tr>

      <tr>
       <td>$a === $b</td>
       <td>全等</td>
       <td><strong><code>true</code></strong>，如果 <var class="varname">$a</var> 等于 <var class="varname">$b</var>，并且它们的类型也相同。</td>
      </tr>

      <tr>
       <td>$a != $b</td>
       <td>不等</td>
       <td><strong><code>true</code></strong>，如果类型转换后 <var class="varname">$a</var> 不等于 <var class="varname">$b</var>。</td>
      </tr>

      <tr>
       <td>$a &lt;&gt; $b</td>
       <td>不等</td>
       <td><strong><code>true</code></strong>，如果类型转换后 <var class="varname">$a</var> 不等于 <var class="varname">$b</var>。</td>
      </tr>

      <tr>
       <td>$a !== $b</td>
       <td>不全等</td>
       <td><strong><code>true</code></strong>，如果 <var class="varname">$a</var> 不等于 <var class="varname">$b</var>，或者它们的类型不同。</td>
      </tr>

      <tr>
       <td>$a &lt; $b</td>
       <td>小与</td>
       <td><strong><code>true</code></strong>，如果 <var class="varname">$a</var> 严格小于 <var class="varname">$b</var>。</td>
      </tr>

      <tr>
       <td>$a &gt; $b</td>
       <td>大于</td>
       <td><strong><code>true</code></strong>，如果 <var class="varname">$a</var> 严格大于 <var class="varname">$b</var>。</td>
      </tr>

      <tr>
       <td>$a &lt;= $b</td>
       <td>小于等于</td>
       <td><strong><code>true</code></strong>，如果 <var class="varname">$a</var> 小于或者等于 <var class="varname">$b</var>。</td>
      </tr>

      <tr>
       <td>$a &gt;= $b</td>
       <td>大于等于</td>
       <td><strong><code>true</code></strong>，如果 <var class="varname">$a</var> 大于或者等于 <var class="varname">$b</var>。</td>
      </tr>

      <tr>
       <td>$a &lt;=&gt; $b</td>
       <td>太空船运算符（组合比较符）</td>
       <td>
        当<var class="varname">$a</var>小于、等于、大于 <var class="varname">$b</var>时
        分别返回一个小于、等于、大于0的 <span class="type">int</span> 值。
       </td>
      </tr>

     </tbody>
    
   </table>

   <p class="para">
    当两个操作对象都是
    <a href="language.types.numeric-strings.html" class="link">数字字符串</a>，
    或一个是数字另一个是
    <a href="language.types.numeric-strings.html" class="link">数字字符串</a>，
    就会自动按照数值进行比较。
    此规则也适用于
    <a href="control-structures.switch.html" class="link">switch</a> 语句。
    当比较时用的是 <code class="literal">===</code> 或 <code class="literal">!==</code>，
    则不会进行类型转换——因为不仅要对比数值，还要对比类型。
   </p>
   <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
   PHP 8.0.0 之前，如果 <span class="type">string</span> 与数字或者数字字符串进行比较，
   则在比较前会将 <span class="type">string</span> 转化为数字。
   在如下示例中会出现不可预料的结果：
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #DD0000">"1"&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">"01"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #DD0000">"10"&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">"1e1"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">100&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">"1e2"</span><span style="color: #007700">);<br /><br />switch&nbsp;(</span><span style="color: #DD0000">"a"</span><span style="color: #007700">)&nbsp;{<br />case&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"0"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />case&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

      <p class="para">以上例程在 PHP 7 中的输出：</p>
      <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
bool(true)
bool(true)
bool(true)
0
</pre></div>
      </div>
      <p class="para">以上例程在 PHP 8 中的输出：</p>
      <div class="example-contents screen">
<div class="cdata"><pre>
bool(false)
bool(true)
bool(true)
bool(true)
a
</pre></div>
      </div>   
     </div>
    </p>
   </div>

   <p class="para">
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;整数<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;-1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;1<br /><br />//&nbsp;浮点数<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">1.5&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">1.5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">1.5&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">2.5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;-1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">2.5&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">1.5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;1<br /><br />//&nbsp;字符串<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;-1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;1<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #DD0000">"aa"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;-1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"zz"&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #DD0000">"aa"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;1<br /><br />//&nbsp;数组<br /></span><span style="color: #007700">echo&nbsp;[]&nbsp;&lt;=&gt;&nbsp;[];&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">]&nbsp;&lt;=&gt;&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">]&nbsp;&lt;=&gt;&nbsp;[];&nbsp;</span><span style="color: #FF8000">//&nbsp;1<br /></span><span style="color: #007700">echo&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">]&nbsp;&lt;=&gt;&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;1<br /></span><span style="color: #007700">echo&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">]&nbsp;&lt;=&gt;&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;-1<br /><br />//&nbsp;对象<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;(object)&nbsp;[</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;(object)&nbsp;[</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;(object)&nbsp;[</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;(object)&nbsp;[</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"c"</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;-1<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;(object)&nbsp;[</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"c"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;(object)&nbsp;[</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;1<br /><br />//&nbsp;不仅仅比较值，而且也会匹配键<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;(object)&nbsp;[</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;(object)&nbsp;[</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;1<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>

     </div>

    </div>
   </p>

   <p class="para">
    对于多种类型，比较运算符根据下表比较（按顺序）。
   </p>
   <table id="language.operators.comparison.types" class="doctable table">
    <caption><strong>比较多种类型</strong></caption>
    
     <thead>
      <tr>
       <th>运算数 1 类型</th>
       <th>运算数 2 类型</th>
       <th>结果</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><span class="type">null</span> 或 <span class="type">string</span></td>
       <td><span class="type">string</span></td>
       <td>将 <strong><code>null</code></strong> 转换为 &quot;&quot;，进行数字或词汇比较</td>
      </tr>

      <tr>
       <td><span class="type">bool</span> 或 <span class="type">null</span></td>
       <td>任何其它类型</td>
       <td>转换为 <span class="type">bool</span>，<strong><code>false</code></strong> &lt; <strong><code>true</code></strong></td>
      </tr>

      <tr>
       <td><span class="type">object</span></td>
       <td><span class="type">object</span></td>
       <td>
        内置类可以定义自己的比较，不同类不能比较，相同的类查看<a href="language.oop5.object-comparison.html" class="link">对象比较</a>
       </td>
      </tr>

      <tr>
       <td><span class="type">string</span>、<span class="type">resource</span>、<span class="type">int</span>、<span class="type">float</span></td>
       <td><span class="type">string</span>、<span class="type">resource</span>、<span class="type">int</span>、<span class="type">float</span></td>
       <td>将字符串和资源转换成数字，按普通数学比较</td>
      </tr>

      <tr>
       <td><span class="type">array</span></td>
       <td><span class="type">array</span></td>
       <td>
        成员越少的数组越小，如果运算数 1 中的键不存在于运算数
        2 中则数组无法比较，否则挨个值比较（见下例）
       </td>
      </tr>

      <tr>
       <td><span class="type">object</span></td>
       <td>任何其它类型</td>
       <td><span class="type">object</span> 总是更大</td>
      </tr>

      <tr>
       <td><span class="type">array</span></td>
       <td>任何其它类型</td>
       <td><span class="type">array</span> 总是更大</td>
      </tr>

     </tbody>
    
   </table>


   <p class="para">
    <div class="example" id="example-112">
     <p><strong>示例 #1 Boolean/null comparison</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Bool&nbsp;和&nbsp;null&nbsp;总是作为&nbsp;bool&nbsp;比较<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;TRUE&nbsp;-&nbsp;same&nbsp;as&nbsp;(bool)1&nbsp;==&nbsp;TRUE<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;TRUE&nbsp;-&nbsp;same&nbsp;as&nbsp;(bool)0&nbsp;==&nbsp;FALSE<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">100&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;FALSE&nbsp;-&nbsp;same&nbsp;as&nbsp;(bool)100&nbsp;&lt;&nbsp;TRUE<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(-</span><span style="color: #0000BB">10&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">);</span><span style="color: #FF8000">//&nbsp;FALSE&nbsp;-&nbsp;same&nbsp;as&nbsp;(bool)-10&nbsp;&lt;&nbsp;FALSE<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">min</span><span style="color: #007700">(-</span><span style="color: #0000BB">100</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;NULL&nbsp;-&nbsp;(bool)NULL&nbsp;&lt;&nbsp;(bool)-100&nbsp;is&nbsp;FALSE&nbsp;&lt;&nbsp;TRUE<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>

   <p class="para">
    <div class="example" id="example-113">
     <p><strong>示例 #2 标准数组比较代码</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;数组是用标准比较运算符或者太空船运算符进行比较的<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">standard_array_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">$op1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$op2</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$op1</span><span style="color: #007700">)&nbsp;&lt;&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$op2</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$op1&nbsp;&lt;&nbsp;$op2<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$op1</span><span style="color: #007700">)&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$op2</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$op1&nbsp;&gt;&nbsp;$op2<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$op1&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$op2</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$op2</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">])&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$op2</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">])&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$op1&nbsp;==&nbsp;$op2<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>

   <div class="warning"><strong class="warning">警告</strong>
    <h1 class="title">比较浮点数</h1>

    <p class="para">
     由于浮点数 <span class="type">float</span> 的内部表达方式，不应比较两个浮点数<span class="type">float</span>是否相等。
    </p>

    <p class="para">
     更多信息参见 <span class="type">float</span>。
    </p>
   </div>

   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <span class="simpara">
     请注意，在比较不同类型的值时，类型转换并不总是很明显，尤其是比较 <span class="type">int</span> 与 <span class="type">bool</span> 或者
     <span class="type">int</span> 与 <span class="type">string</span>。因此，在大多数情况下，通常建议使用 <code class="literal">===</code> 和
     <code class="literal">!==</code> 进行比较而不是 <code class="literal">==</code> 和 <code class="literal">!=</code>。
    </span>
   </p></blockquote>

   <div class="sect2" id="language.operators.comparison.incomparable">
    <h3 class="title">不能比较的值</h3>
    <p class="simpara">
     虽然恒等比较（<code class="literal">===</code> 和
     <code class="literal">!==</code>）可以应用于任意值，其它比较运算符应该仅用于可比较的值。不能比较的值的比较的结果是不确定的，不应依赖。
    </p>
   </div>

   <div class="sect2">
    <h3 class="title">参见</h3>
    <p class="para">
     <ul class="simplelist">
      <li class="member"><span class="function"><a href="function.strcasecmp.html" class="function">strcasecmp()</a></span></li>
      <li class="member"><span class="function"><a href="function.strcmp.html" class="function">strcmp()</a></span></li>
      <li class="member"><a href="language.operators.array.html" class="link">数组运算符</a></li>
      <li class="member"><a href="language.types.html" class="link">类型</a></li>
     </ul>
    </p>
   </div>

  <div class="sect2" id="language.operators.comparison.ternary">
    <h3 class="title">三元运算符</h3>
    <p class="para">
     另一个条件运算符是“?:”（或三元）运算符 。
     <div class="example" id="example-114">
      <p><strong>示例 #3 赋默认值</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;三元运算符的例子<br /></span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">=&nbsp;(empty(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">]))&nbsp;?&nbsp;</span><span style="color: #DD0000">'default'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">//&nbsp;以上等同于以下的&nbsp;&nbsp;if/else&nbsp;语句<br /></span><span style="color: #007700">if&nbsp;(empty(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">]))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'default'</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">];<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
     表达式 <code class="literal">(expr1) ? (expr2) : (expr3)</code> 在
     <span class="replaceable">expr1</span> 求值为 <strong><code>true</code></strong>
     时的值为 <span class="replaceable">expr2</span>，在
     <span class="replaceable">expr1</span> 求值为 <strong><code>false</code></strong> 时的值为
     <span class="replaceable">expr3</span>。
    </p>
    <p class="para">
     可以省略三元运算符中间那部分。表达式
     <code class="literal">expr1 ?: expr3</code> 等同于如果
     <span class="replaceable">expr1</span> 求值为 <strong><code>true</code></strong> 时返回
     <span class="replaceable">expr1</span> 的结果，否则返回
     <span class="replaceable">expr3</span>。<span class="replaceable">expr1</span> 在这里仅执行一次。
    </p>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <span class="simpara">
      注意三元运算符是个语句，因此其求值不是变量，而是语句的结果。如果想通过引用返回一个变量这点就很重要。在一个通过引用返回的函数中语句
      <code class="literal">return $var == 42 ? $a : $b;</code> 将不起作用，以后的 PHP
      版本会为此发出一条警告。
     </span>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <p class="para">
      建议避免将三元运算符堆积在一起使用。和其他语言相比，
      当在单个表达式中使用多个未加括号的三元运算符时会造成
      PHP 运算结果不清晰。
      甚至在 PHP 8.0.0 之前，三元运算符是从左到右执行的，
      而大多数其他编程语言是从右到左的。
      自 PHP 7.4.0 起，弃用依靠左联。
      PHP 8.0.0 起，三元运算符是非关联的。
      <div class="example" id="example-115">
       <p><strong>示例 #4 不清晰的三元运算符行为</strong></p>
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;乍看起来下面的输出是&nbsp;'true'<br /></span><span style="color: #007700">echo&nbsp;(</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #DD0000">'true'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #DD0000">'t'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">'f'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;然而，上面语句的实际输出是't'，因为在&nbsp;PHP&nbsp;8.0.0&nbsp;之前三元运算符是左联的<br /><br />//&nbsp;下面是与上面等价的语句，但更清晰<br /></span><span style="color: #007700">echo&nbsp;((</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #DD0000">'true'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">'false'</span><span style="color: #007700">)&nbsp;?&nbsp;</span><span style="color: #DD0000">'t'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">'f'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;这里可以看到第一个表达式的计算结果是&nbsp;“true”，第二个表达式的计算结果为&nbsp;(bool)true，<br />//&nbsp;因此返回第二个三元表达式的&nbsp;true&nbsp;分支。<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
       </div>

      </div>
     </p>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <p class="para">
      然而，短三元运算符（<code class="literal">?:</code>）的链接是稳定且合理的。他将第一个参数进行求值，结果为非假值。注意，未定义的值将会引发警告。
      <div class="example" id="example-116">
       <p><strong>示例 #5 Short-ternary chaining</strong></p>
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">?:&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">?:&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">?:&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">?:&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">?:&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">?:&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//2<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">?:&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">?:&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">?:&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//3<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
       </div>

      </div>
     </p>
    </p></blockquote>
   </div>

   <div class="sect2" id="language.operators.comparison.coalesce">
    <h3 class="title">NULL 合并运算符</h3>
    <p class="para">
     另一个简略运算符是 &quot;??&quot; （NULL 合并）运算符。
     <div class="example" id="example-117">
      <p><strong>示例 #6 设置默认值</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;NULL&nbsp;合并运算符的例子<br /></span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">]&nbsp;??&nbsp;</span><span style="color: #DD0000">'default'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;以上例子等同于于以下&nbsp;if/else&nbsp;语句<br /></span><span style="color: #007700">if&nbsp;(isset(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">]))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">];<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'default'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
     当 <span class="replaceable">expr1</span> 为  <strong><code>null</code></strong>，表达式 <code class="literal">(expr1) ?? (expr2)</code> 等同于
     <span class="replaceable">expr2</span>，否则为 <span class="replaceable">expr1</span>。
    </p>
    <p class="para">
     尤其要注意，当不存在左侧的值时，此运算符也和 <span class="function"><a href="function.isset.html" class="function">isset()</a></span> 一样不会产生警告。
     对于 array 键尤其有用。
    </p>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <span class="simpara">
      请注意：NULL 合并运算符是一个表达式，产生的也是表达式结果，而不是变量。
      返回引用变量时需要强调这一点。
      因此，在返回引用的函数里就无法使用这样的语句：<code class="literal">return $foo ?? $bar;</code>，还会提示警告。
     </span>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <p class="para">
      null 合并运算符的优先级较低。这意味着如果将它与其它运算符（比如字符串链接或者算术运算符）混合使用，可能需要括号。
     </p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;$name&nbsp;未定义，引发警告。<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">'Mr.&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">??&nbsp;</span><span style="color: #DD0000">'Anonymous'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;打印&nbsp;"Mr.&nbsp;Anonymous"<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">'Mr.&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;(</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">??&nbsp;</span><span style="color: #DD0000">'Anonymous'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </p></blockquote>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <p class="para">
      请注意，NULL 合并运算符支持简单的嵌套：
      <div class="example" id="example-118">
       <p><strong>示例 #7 嵌套 NULL 合并运算符</strong></p>
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$baz&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$qux&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">??&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">??&nbsp;</span><span style="color: #0000BB">$baz&nbsp;</span><span style="color: #007700">??&nbsp;</span><span style="color: #0000BB">$qux</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;1<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
       </div>

      </div>
     </p>
    </p></blockquote>
   </div>
  </div></div></div></body></html>